package com.mrl.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList list = new DoubleLinkedList();
        // 测试添加
        list.add(new DoubleLinkedListNode(1, "我是一号", "昵称1"));
        list.add(new DoubleLinkedListNode(2, "我是二号", "昵称2"));
        list.add(new DoubleLinkedListNode(3, "我是三号", "昵称3"));
        list.add(new DoubleLinkedListNode(4, "我是四号", "昵称4"));
        System.out.println("===========测试添加===========");
        list.printList();

        // 测试删除
        list.remove(2);
        System.out.println("==========删除节点后==========");
        list.printList();

        // 测试修改
        list.update(new DoubleLinkedListNode(3, "我是新的三号", "新的昵称3"));
        System.out.println("==========修改节点后==========");
        list.printList();

    }
}

/**
 * 双向链表
 */
class DoubleLinkedList {
    private DoubleLinkedListNode head;

    public DoubleLinkedListNode getHead() {
        return head;
    }

    public void setHead(DoubleLinkedListNode head) {
        this.head = head;
    }

    DoubleLinkedList() {
        head = new DoubleLinkedListNode(0, "", "");
    }

    /**
     * 添加元素到双向链表
     * @param node 待添加的元素
     */
    public void add(DoubleLinkedListNode node) {
        DoubleLinkedListNode tmp = head;
        while (tmp.getNext() != null) {
            tmp = tmp.getNext();
        }
        tmp.setNext(node);
        node.setPre(tmp);
    }

    /**
     * 从链表中删除节点
     * @param id 要删除节点的id
     */
    public void remove(int id) {
        if (head.getNext() == null) {
            System.out.println("无法删除元素：该链表为空!");
            return;
        }
        DoubleLinkedListNode tmp = head.getNext();
        while (tmp != null) {
            // 判断是否为要删除项
            if (tmp.getId() == id) {
                tmp.getPre().setNext(tmp.getNext());
                tmp.getNext().setPre(tmp.getPre());
                return;
            }
            tmp = tmp.getNext();
        }
        // 如果走到这一步说明没有找到要删除的元素
        System.out.println("无法删除元素：未找到元素！");
    }

    /**
     * 修改节点
     * @param node 要修改的节点及其数据
     */
    public void update(DoubleLinkedListNode node) {
        // 判断是否为空链表
        if (head.getNext() == null) {
            System.out.println("修改节点失败：该链表为空！");
            return;
        }
        // 遍历链表
        DoubleLinkedListNode tmp = head.getNext();
        while (tmp != null) {
            if (tmp.getId() == node.getId()) {
                tmp.setName(node.getName());
                tmp.setNickname(node.getNickname());
                return;
            }
            tmp = tmp.getNext();
        }
        // 如果走到这说明没有找到目标节点
        System.out.println("修改节点失败：没有找到目标节点！");
    }

    /**
     * 打印链表
     */
    public void printList() {
        if (head.getNext() == null) {
            System.out.println("此链表为空！");
            return;
        }
        DoubleLinkedListNode tmp = head.getNext();
        while (tmp != null) {
            System.out.println(tmp);
            tmp = tmp.getNext();
        }
    }
}

/**
 * 链表节点
 */
class DoubleLinkedListNode {
    private int id;
    private String name;
    private String nickname;
    private DoubleLinkedListNode next;
    private DoubleLinkedListNode pre;

    DoubleLinkedListNode(int id, String name, String nickname) {
        this.id = id;
        this.name = name;
        this.nickname = nickname;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public DoubleLinkedListNode getNext() {
        return next;
    }

    public void setNext(DoubleLinkedListNode next) {
        this.next = next;
    }

    public DoubleLinkedListNode getPre() {
        return pre;
    }

    public void setPre(DoubleLinkedListNode pre) {
        this.pre = pre;
    }

    @Override
    public String toString() {
        return "DoubleHeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
